package com.kws.merchant.mapper;

import com.kws.merchant.domain.GoodsView;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;


/**
 * 商品浏览记录表(GoodsView)表数据库访问层
 *
 * @author yxy
 * @since 2024-11-04 09:11:23
 */

public interface GoodsViewMapper extends BaseMapper<GoodsView> {

    /**
     * 查询访客数量
     * @param userId
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_goods_view where merchant_id='${userId}' and DATE(create_time) like '${time}'")
    Double merchantVisitor(@Param("userId") Long userId,@Param("time") String time);

    /**
     * 查询访问量
     * @param userId
     * @param time
     * @return
     */
    @Select("select  count(*) from tb_goods_view where merchant_id='${userId}' and DATE(create_time) like '${time}'")
    Double merchantViewByTime(@Param("userId") Long userId,@Param("time") String time);

    /**
     * 根据类型时间查询访客数
     * @param companyId
     * @param time
     * @param type
     * @return
     */
    @Select("select  count(*) from tb_goods_view where merchant_id='${companyId}' and type=${type} and DATE(create_time) like '${time}'")
    int merchantVisitorByTimeByType(@Param("companyId") Long companyId,@Param("time") String time,@Param("type") Integer type);

    /**
     * 总访客
     * @param userId
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_goods_view where merchant_id='${userId}'")
    Double countVisitor(@Param("userId") Long userId);

    /**
     * 总浏览量
     * @param userId
     * @return
     */
    @Select("select  count(*) from tb_goods_view where merchant_id='${userId}'")
    Double countView(@Param("userId") Long userId);

    /**
     * 根据年-月查询浏览时间
     * @param companyId
     * @return
     */
    @Select("select  count(view_time) from tb_goods_view where merchant_id='${companyId}' and type='${type}' and YEAR(create_time)='${year}' and MONTH(create_time)='${month}';")
    Double countTimeByYearAndMonth(@Param("companyId") Long companyId,@Param("year") int year,@Param("month") int month,@Param("type")int type);

    /**
     * 根据年-月查询浏览量人
     * @param companyId
     * @param year
     * @param month
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_goods_view where merchant_id='${companyId}' and type='${type}' and YEAR(create_time)='${year}' and MONTH(create_time)='${month}';")
    Double countPeopleByYearAndMonth(@Param("companyId") Long companyId,@Param("year") int year,@Param("month") int month,@Param("type")int type);

    /**
     * 根据年-月查询浏览量
     * @param companyId
     * @param year
     * @param month
     * @param type
     * @return
     */
    @Select("select  count(*) from tb_goods_view where merchant_id='${companyId}' and type='${type}' and YEAR(create_time)='${year}' and MONTH(create_time)='${month}';")
    Double countViewTimeByYearAndMonth(@Param("companyId") Long companyId,@Param("year") int year,@Param("month") int month,@Param("type")int type);

    /**
     * 按年月日查询小时浏览量
     * @param companyId
     * @param type
     * @param sql
     * @return
     */
    @Select("select  count(*) from tb_goods_view where merchant_id='${companyId}' and type='${type}' ${sql};")
    Double countViewBySql(@Param("companyId") Long companyId,@Param("type") Integer type,@Param("sql") String sql);

    /**
     * 按年月日查询小时停留时间-浏览时间
     * @param companyId
     * @param type
     * @param sql
     * @return
     */
    @Select("select  IFNULL(sum(view_time),0) from tb_goods_view where merchant_id='${companyId}' and type='${type}' ${sql};")
    Double countViewTimeBySql(@Param("companyId") Long companyId,@Param("type") Integer type,@Param("sql") String sql);

    /**
     * 根据sql查询访问量
     * @param companyId
     * @param type
     * @param sql
     * @return
     */
    @Select("select  count(DISTINCT user_id) from tb_goods_view where merchant_id='${companyId}' and type='${type}' ${sql};")
    Double countViewByPeopleSql(@Param("companyId") Long companyId,@Param("type") Integer type,@Param("sql") String sql);
}
